package display3d.alternativa {
	import alternativa.engine3d.primitives.Box;
	import alternativa.Alternativa3D;
	import alternativa.engine3d.loaders.Parser3DS;
	import flash.display.BitmapData;
	import alternativa.engine3d.materials.TextureMaterial;
	import jazztures.utils.GeometryUtils;
	import alternativa.engine3d.primitives.GeoSphere;
	import alternativa.engine3d.materials.FillMaterial;
	import alternativa.engine3d.loaders.ParserCollada;
	import alternativa.engine3d.objects.Mesh;
	import alternativa.engine3d.core.Object3DContainer;
	import flash.geom.Vector3D;


	/**
	 * @author jao
	 */
	public class Focus extends Object3DContainer {
		[Embed(source="/../assets/focus_model.dae", mimeType="application/octet-stream")]
		private var BodyModel : Class;
		
		[Embed(source="/../assets/focus_texture.jpg")]
		private var BodyTexture:Class;
		
		[Embed(source="/../assets/wheel_texture.png")]
		private var WheelTexture:Class;
		
	
		private var _lights:Array;
		private var acceleration:int = 0;
		private var speed:Number = 0;
		
		
		private var bodyContainer:Object3DContainer;
		private var flContainer:Object3DContainer;
		private var frContainer:Object3DContainer;
		private var rlContainer:Object3DContainer;
		private var rrContainer:Object3DContainer;
		private var body:Mesh;
		private var frontLeft:Mesh;
		private var frontRight:Mesh;
		private var rearLeft:Mesh;
		private var rearRight:Mesh;
		
		private var direction:Number = 0;
		
		public function Focus() {
			var parser : ParserCollada = new ParserCollada();
			parser.parse(new XML(new BodyModel()), null, false);
			
			bodyContainer = new Object3DContainer();
			addChild(bodyContainer);
			body = Mesh(parser.getObjectByName("Body"));
			body.setMaterialToAllFaces(new TextureMaterial(BitmapData(new BodyTexture().bitmapData),false, true, 0, 2));
			bodyContainer.addChild(body);
			body.rotationZ = Math.PI;
			body.rotationY = Math.PI/2;
			body.z = -62;

			
			flContainer = new Object3DContainer();
			flContainer.x = -60;
			flContainer.y = -12;
			flContainer.z = 0;
			addChild(flContainer);
			frontLeft = Mesh(parser.getObjectByName("Wheel_FL"));
			frontLeft.setMaterialToAllFaces(new TextureMaterial(BitmapData(new WheelTexture().bitmapData),false, true, 0, 2));
			flContainer.addChild(frontLeft);
			
			frContainer = new Object3DContainer();
			frContainer.x = 60;
			frContainer.y = -12;
			frContainer.z = 0;
			addChild(frContainer);
			frontRight = Mesh(parser.getObjectByName("Wheel_FR"));
			frontRight.setMaterialToAllFaces(new TextureMaterial(BitmapData(new WheelTexture().bitmapData),false, true, 0, 2));
			frContainer.addChild(frontRight);
			
			rlContainer = new Object3DContainer();
			rlContainer.x = -60;
			rlContainer.y = -12;
			rlContainer.z = -192;
			addChild(rlContainer);
			rearLeft = Mesh(parser.getObjectByName("Wheel_RL"));
			rearLeft.setMaterialToAllFaces(new TextureMaterial(BitmapData(new WheelTexture().bitmapData),false, true, 0, 2));
			rlContainer.addChild(rearLeft);
			
			rrContainer = new Object3DContainer();
			rrContainer.x = 60;
			rrContainer.y = -12;
			rrContainer.z = -192;
			addChild(rrContainer);
			rearRight = Mesh(parser.getObjectByName("Wheel_RR"));
			rearRight.setMaterialToAllFaces(new TextureMaterial(BitmapData(new WheelTexture().bitmapData),false, true, 0, 2));
			rrContainer.addChild(rearRight);
			
		}
		
		public function accelerate(amount:Number=1):void{
			acceleration +=amount;
		}
		
		public function decelerate():void{
			acceleration = 0;
		}
		
		public function turn(side:int):void{
			if(side !=0) direction+=(side*15); else direction /=3;
			direction = Math.min(Math.max(direction, -30/(speed/10), -30), 30/(speed/10), 30);
		}
		
		public function brake():void{
			acceleration = -3;
		}
		
		public function update():void{
			speed +=acceleration;
			speed-=1;
			speed = Math.min(Math.max(speed, 0), 30);
			
			frontRight.rotationZ = -(frontLeft.rotationZ += GeometryUtils.toRadians(speed));
			frContainer.rotationY = Math.PI + (flContainer.rotationY = GeometryUtils.toRadians(-90 + direction));
			
			rearLeft.rotationZ = frontLeft.rotationZ;
			rlContainer.rotationY = -Math.PI/2;
			
			rearRight.rotationZ = frontRight.rotationZ;
			rrContainer.rotationY = Math.PI/2;
			
			body.rotationZ = GeometryUtils.toRadians(180 + direction*(speed/100));
			bodyContainer.rotationX = GeometryUtils.toRadians(speed/50);
			this.rotationY += GeometryUtils.toRadians(direction)*(speed/100);
			this.x+= Math.sin(rotationY)*speed;
			this.z+= Math.cos(rotationY)*speed;
		}
		
		public function set lights(value:Array):void{
			_lights = value;
		}
	}
}
